package com.hmkj.core.mapper.order;

import com.hmkj.common.baseMapper.GenericMapper;
import com.hmkj.core.model.order.OrderSimulateModel;
import com.hmkj.core.po.order.OrderSimulate;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 模拟策略表相关Dao接口<br>
 *
 * @author eproo
 * @date 2017-11-10 11:33:05
 */
public interface OrderSimulateMapper extends GenericMapper<OrderSimulate, Integer> {

    @Select("SELECT\n" +
            "\tt.*,u.user_name AS 'u.userName',u.mobile AS 'u.mobile',iu.user_name AS 'iu.userName',iu.mobile AS 'iu.mobile',ag.name AS 'ag.name'\n" +
            "FROM\n" +
            "\tbiz_order_simulate t \n" +
            "LEFT JOIN biz_user u ON t.user_id = u.id \n" +
            "LEFT JOIN biz_user iu ON t.invest_id = iu.id\n" +
            "LEFT JOIN biz_agent ag ON t.agent_id = ag.id\n" +
            "WHERE 1 = 1 ${whereSql} ${sortSql} LIMIT ${nowPage}, ${pageSize}")
    List<OrderSimulateModel> findListBySqlJoin(Map<String, Object> params);

    @Select("SELECT count(1) FROM " +
            "\tbiz_order_simulate t \n" +
            "LEFT JOIN biz_user u ON t.user_id = u.id \n" +
            "LEFT JOIN biz_user iu ON t.invest_id = iu.id\n" +
            "LEFT JOIN biz_agent ag ON t.agent_id = ag.id\n" +
            "WHERE 1 = 1 ${whereSql} ")
    long countBySqlJoin(Map<String, Object> params);

    /**
     * 前台获取战绩
     *
     * @param params
     * @return
     */
    @Select("<script> SELECT COUNT(1) FROM biz_order_simulate t WHERE t.del_flag = 1 AND t.user_id = #{userId} AND t.status = #{status} AND t.del_flag = #{delFlag} " +
            " <if test=\"agentId != null and agentId != ''\"> AND t.`agent_id` = #{agentId} </if>" +
            " ORDER BY t.settle_time DESC </script>")
    Integer findCountByUserId(Map<String, Object> params);

    /**
     * 前台获取战绩
     *
     * @param params
     * @return
     */
    @Select("<script> SELECT t.* FROM biz_order_simulate t WHERE t.del_flag = 1 AND t.user_id = #{userId} AND t.status = #{status} AND t.del_flag = #{delFlag} " +
            " <if test=\"agentId != null and agentId != ''\"> AND t.`agent_id` = #{agentId} </if>" +
            " ORDER BY t.settle_time DESC </script>")
    List<OrderSimulateModel> findListByUserId(Map<String, Object> params);

    /**
     * 前台获取跟单列表
     *
     * @return
     */
    @Select("<script> SELECT COUNT(1) FROM (SELECT COUNT(1) FROM biz_order_simulate t LEFT JOIN biz_user u ON t.user_id = u.id " +
            " WHERE 1 = 1 AND t.`status` = #{status} AND t.del_flag = #{delFlag} AND u.type = #{type} <if test=\"agentId != null and agentId != ''\"> AND t.`agent_id` = #{agentId} </if> GROUP BY t.user_id) t1 </script>")
    Integer findCountForOrderSimList(Map<String, Object> params);

    /**
     * 前台获取跟单列表
     *
     * @return
     */
    @Select("<script> SELECT COUNT(1) AS orderNum, SUM(t.profit)/COUNT(1) AS averageProfit,SUM(t.profit_point) AS totalProfitPoint, t.user_id,u.user_name AS 'u.userName',IFNULL(u.avatar_url,'') AS 'u.avatarUrl' FROM biz_order_simulate t LEFT JOIN biz_user u ON t.user_id = u.id " +
            " WHERE 1 = 1 AND t.`status` = #{status} AND t.del_flag = #{delFlag} AND u.type = #{type} <if test=\"agentId != null and agentId != ''\"> AND t.`agent_id` = #{agentId} </if> GROUP BY t.user_id ORDER BY averageProfit DESC </script>")
    List<OrderSimulateModel> findListForOrderSimList(Map<String, Object> params);

    /**
     * 获取非操盘手下的模拟订单
     */
    @Select("SELECT t.* FROM biz_order_simulate t LEFT JOIN biz_user u ON t.user_id = u.id WHERE u.type <> #{type} AND t.commo_id = #{commoId} AND t.`status` = #{status} AND t.del_flag = #{delFlag}")
    List<OrderSimulate> findNotTradeOrderSimulateList(Map<String, Object> params);

    /**
     * 查询同一订单拆分的所有订单号
     *
     * @param orderNo 原始订单号
     * @return
     */
    @Select("SELECT order_no from biz_order_simulate where order_no like concat(#{orderNo},'%') and del_flag = 1")
    List<String> selectOrderNos(@Param(value = "orderNo") String orderNo);

    /**
     * 查询模拟订单数量（有效的）
     *
     * @param userId 用户ID
     * @return
     */    /**
     * 获取非操盘手下的模拟订单
     */
    @Select("SELECT t.* FROM biz_order_simulate t WHERE t.`status` in (3,4,5,6) AND t.user_id = #{userId} AND t.del_flag = 1")
    List<OrderSimulate> selectSucByUID(@Param(value = "userId") Integer userId);

    /**
     * 前台模拟跟单持仓列表查询
     *
     * @param params
     * @return
     */
    @Select("<script>SELECT COUNT(1) FROM biz_order_simulate t WHERE t.del_flag = #{delFlag} AND t.`status` in (#{status}<if test=\"status2 != null and status2 != ''\"> ,#{status2}</if>) AND t.user_id = #{userId} AND t.follow = #{follow}</script>")
    Integer findCountForOrderList(Map<String, Object> params);

    /**
     * 前台模拟持仓列表查询
     *
     * @param params
     * @return
     */
    @Select("<script>SELECT t.* FROM biz_order_simulate t WHERE t.del_flag = #{delFlag} AND t.`status` in (#{status}<if test=\"status2 != null and status2 != ''\"> ,#{status2}</if>) AND t.user_id = #{userId} AND t.follow = #{follow} ORDER BY t.create_time DESC</script>")
    List<OrderSimulateModel> findListForOrderList(Map<String, Object> params);

    /**
     * 查询待流单的模拟订单
     */
    @Select("SELECT t.* FROM biz_order_simulate t WHERE t.`status` = #{status} AND TIMESTAMPDIFF(SECOND,t.create_time,NOW()) > 5  AND t.del_flag = 1")
    List<OrderSimulate> selectWaitFlowOrderSimulate(Map<String, Object> params);

    /**
     * 查询达到止盈止损线的订单
     *
     * @param np   最新价
     * @param code 品种类型
     * @return
     */
    @Select("SELECT * FROM biz_order_simulate  WHERE del_flag = 1 AND `status` = 3 AND code = #{code} AND ((direction = 1 AND ( lose_stop_price >= #{np} OR profit_stop_price <= #{np})) OR (direction = 2 AND ( lose_stop_price <= #{np} OR profit_stop_price >= #{np})))")
    List<OrderSimulate> selectByLostProfit(@Param(value = "np")BigDecimal np, @Param(value = "code")String code);
}
